package com.hafu.view.ShangPin;
import com.hafu.model.SpInfo;
import com.hafu.service.SpInfoService;
import com.hafu.util.StringUtil;
import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/*
 * Created by JFormDesigner on Tue Apr 01 16:19:03 CST 2025
 */



/**
 * @author Administrator
 */
public class ManageShangPinFrm extends JFrame {
    private SpInfoService spInfoService = new SpInfoService();
    public ManageShangPinFrm() {
        initComponents();
        fillTable();
    }
    private void fillTable() {
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        dtm.setRowCount(0);
        ArrayList<SpInfo> spInfos = spInfoService.getSpInfoByName(new SpInfo());
        for (SpInfo spInfo : spInfos) {
            Object[] rowData = {spInfo.getId(), spInfo.getName(), spInfo.getPrice()};
            dtm.addRow(rowData);
        }
    }
    private void button1(ActionEvent e) {
        String name = textField1.getText();
        SpInfo spInfo = new SpInfo(name);
        ArrayList<SpInfo> spInfos = spInfoService.getSpInfoByName(spInfo);
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        dtm.setRowCount(0);
        if (spInfos.size() > 0){
            for (SpInfo spInfo1 : spInfos) {
                Object[] rowData = {spInfo1.getId(), spInfo1.getName(), spInfo1.getPrice()};
                dtm.addRow(rowData);
            }
        }else {
            JOptionPane.showMessageDialog(null,"没有找到该课程");
        }
    }
    private void table1MouseClicked(MouseEvent e) {
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtm.getValueAt(row, 0);
        String name =(String)dtm.getValueAt(row, 1);
        BigDecimal price =(BigDecimal)dtm.getValueAt(row, 2);
        textField2.setText(name);
        textField3.setText(String.valueOf(price));
    }
    private void button2(ActionEvent e) {
        String name = textField2.getText();
        String price = textField3.getText();

        if (StringUtil.isEmpty(name)){
            JOptionPane.showMessageDialog(null, "课程名称不能为空");
            return;
        }
        if (StringUtil.isEmpty(price)){
            JOptionPane.showMessageDialog(null, "课程价格不能为空");
            return;
        }

        BigDecimal price1 = new BigDecimal(price);
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtm.getValueAt(row, 0);
        SpInfo spInfo = new SpInfo(chooseId,name,price1);
        Boolean aBoolean = spInfoService.updateSpInfo(spInfo);
        if (aBoolean){
            JOptionPane.showMessageDialog(null,"修改成功");
            fillTable();
        }else {
            JOptionPane.showMessageDialog(null,"修改失败");
        }
    }
    private void button3(ActionEvent e) {
        int row = table1.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(null, "请先选择一行数据！");
            return;
        }

        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int id = (int) dtm.getValueAt(row, 0);
        Boolean isDeleteSpInfo = spInfoService.deleteSpInfo(id);

        if (isDeleteSpInfo) {
            JOptionPane.showMessageDialog(null, "删除成功");
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(null, "删除失败");
        }
    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - aaaa
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label2 = new JLabel();
        textField2 = new JTextField();
        label3 = new JLabel();
        textField3 = new JTextField();
        button2 = new JButton();
        button3 = new JButton();

        //======== this ========
        setTitle("\u7ba1\u7406\u8bfe\u7a0b\u4fe1\u606f");
        var contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u8bfe\u7a0b\u540d\u79f0");

        //---- button1 ----
        button1.setText("\u67e5\u8be2");
        button1.addActionListener(e -> button1(e));

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null},
                    {null, null, null},
                },
                new String[] {
                    "\u7f16\u53f7", "\u8bfe\u7a0b\u540d\u79f0", "\u8bfe\u7a0b\u4ef7\u683c/\u8bfe\u65f6"
                }
            ));
            scrollPane1.setViewportView(table1);
        }

        //---- label2 ----
        label2.setText("\u8bfe\u7a0b\u540d\u79f0");

        //---- label3 ----
        label3.setText("\u8bfe\u7a0b\u4ef7\u683c/\u8bfe\u65f6");

        //---- button2 ----
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> {
			button2(e);
			button2(e);
		});

        //---- button3 ----
        button3.setText("\u5220\u9664");
        button3.addActionListener(e -> {
			button3(e);
			button3(e);
		});

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addGap(3, 479, Short.MAX_VALUE)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(button2)
                        .addComponent(button3))
                    .addGap(91, 91, 91))
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addGap(0, 24, Short.MAX_VALUE)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(label3)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(textField3, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)))
                    .addGap(432, 432, 432))
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(30, 30, 30)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(label2)
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addComponent(label1)
                                    .addGap(77, 77, 77)
                                    .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 154, GroupLayout.PREFERRED_SIZE)
                                    .addGap(99, 99, 99)
                                    .addComponent(button1))))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(70, 70, 70)
                            .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(126, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label1)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button1))
                    .addGap(33, 33, 33)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 222, GroupLayout.PREFERRED_SIZE)
                    .addGap(34, 34, 34)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(button2)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label2))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label3)
                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button3))
                    .addContainerGap(58, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - aaaa
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JTextField textField2;
    private JLabel label3;
    private JTextField textField3;
    private JButton button2;
    private JButton button3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on

}